package com.sgcc.pda.h5.businessHw;

import android.util.Log;

import com.sgcc.pda.CommonResultsFactory;
import com.sgcc.pda.common.aidl.CommonCallback;
import com.sgcc.pda.common.aidl.CommonParams;
import com.sgcc.pda.common.aidl.CommonResults;

import java.lang.reflect.Method;

/**
 * @author superFan
 * date: 2019/8/22/022 18:03
 * Copyright (c) 2019 hezhongweiqi.Co.Ltd. All rights reserved.
 * description: H5业务 标签 蓝牙超高频 服务
 */
public class LableUHFBinderUtil extends BaseBusinessHwBinderUtil {
    private static final String TAG = "LableUHFBinderUtil";
    public static final String CLASSNAME = "com.sgcc.pda.h5.businessHw.LableUHFBinderUtil";

    private LableUHFBinderUtil() { }

    private static class Instance {
        private static final LableUHFBinderUtil mInstance = new LableUHFBinderUtil();
    }

    /**
     * 单例
     */
    public static LableUHFBinderUtil getInstance() {
        return LableUHFBinderUtil.Instance.mInstance;
    }

    public void unRegisterBluetoothReceiver() {
        postToPlugin("LableUHFManager.unRegisterBluetoothListener", null, null);
    }

    public CommonResults initBluetooth(CommonParams params, CommonCallback callback) {
        CommonResults commonResults = null;
        commonResults = postToPlugin("LableUHFManager.initBluetooth", params, callback);
        return commonResults;
    }

    /**
     * 连接蓝牙
     *
     * @param commonParams
     * @param commonCallback
     * @return
     */
    public CommonResults connectBluetooth(final CommonParams commonParams, final CommonCallback commonCallback) {
        CommonResults commonResults = postToPlugin("LableUHFManager.connectBluetooth", commonParams, commonCallback);
        commonCallback.callback(commonResults);
        return commonResults;
    }

    /**
     * 断开蓝牙
     *
     * @param commonParams
     * @param commonCallback
     * @return
     */
    public CommonResults disConnectBluetooth(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = postToPlugin("LableUHFManager.disConnectBluetooth", commonParams, commonCallback);
        commonCallback.callback(commonResults);
        return commonResults;
    }

    /**
     * 盘点
     *
     * @param commonParams
     * @param commonCallback
     * @return
     */
    public CommonResults inventory(CommonParams commonParams, final CommonCallback commonCallback) {
        CommonResults commonResults = postToPlugin("LableUHFManager.inventory", commonParams, commonCallback);
        /*FixedThreadUtil.executeThread(new Runnable() {
            @Override
            public void run() {
                LableUHFUtil.getInstance().inventory(new InventoryWorkerUtil.InventroryListener() {
                    @Override
                    public void onTagInventory(String uiiStr, final int rssi) {
                        String tag = uiiStr.replace(" ", "").trim();
                        //正则判断，是否最低28位的数字
                        boolean isMatch = Pattern.compile("^\\d{28,}$").matcher(tag).matches();
                        if (!isMatch) {
                            //条码不全，或非数字条码
                            return;
                        }
                        String substring1 = uiiStr.substring(4, 26).substring(16);
                        final String result;
                        if ("000000".equals(substring1)) {
                            result = uiiStr.substring(4, 20);
                        } else {
                            result = uiiStr.substring(4, 26);
                        }
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                CommonResults commonResults = CommonResultsFactory.successResults();
                                commonResults.addResult("uiiStr", result);
                                commonResults.addResult("rssi", rssi);
                                commonCallback.callback(commonResults);
                            }
                        });
                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });*/
        return commonResults;
    }

    /**
     * 停止盘点
     *
     * @return
     */
    public CommonResults disInventory(CommonParams commonParams, final CommonCallback commonCallback) {
        CommonResults commonResults = postToPlugin("LableUHFManager.disInventory", commonParams, commonCallback);
        return commonResults;
    }

//    public void unRegisterBluetoothReceiver(Context context) {
//        LableUHFUtil.getInstance().unRegisterBluetoothListener(context);
//    }
//
//    public void init(Context context, BluetoothConnectListener connectListener) {
//        LableUHFUtil.getInstance().init(context);
//        LableUHFUtil.getInstance().registerBluetoothReceiver(context);//注册广播
//        LableUHFUtil.getInstance().setConnectListener(connectListener);//接收回调
//    }
//
//    /**
//     * 连接蓝牙
//     *
//     * @param commonParams
//     * @param commonCallback
//     * @return
//     */
//    public CommonResults connectBluetooth(CommonParams commonParams, final CommonCallback commonCallback) {
//        final CommonResults commonResults = CommonResultsFactory.successResults();
//        final String bluetoothAddr = commonParams.getString("bluetoothAddr");//蓝牙地址
//        FixedThreadUtil.executeThread(new Runnable() {
//            @Override
//            public void run() {
//                LableUHFUtil.getInstance().connectBluetooth(bluetoothAddr);
//            }
//        });
//        commonCallback.callback(commonResults);
//        return commonResults;
//    }
//
//    /**
//     * 断开蓝牙
//     *
//     * @param commonParams
//     * @param commonCallback
//     * @return
//     */
//    public CommonResults disConnectBluetooth(CommonParams commonParams, CommonCallback commonCallback) {
//        CommonResults commonResults = CommonResultsFactory.successResults();
//        new Thread() {
//            @Override
//            public void run() {
//                LableUHFUtil.getInstance().disConnectBluetooth();
//            }
//        }.run();
//        commonCallback.callback(commonResults);
//        return commonResults;
//    }
//
//    /**
//     * 盘点
//     *
//     * @param commonParams
//     * @param commonCallback
//     * @return
//     */
//    public CommonResults inventory(CommonParams commonParams, final CommonCallback commonCallback) {
//        Log.d(TAG, "inventory: 开始盘存");
//        CommonResults commonResults = CommonResultsFactory.successResults();
//        FixedThreadUtil.executeThread(new Runnable() {
//            @Override
//            public void run() {
//                LableUHFUtil.getInstance().inventory(new InventoryWorkerUtil.InventroryListener() {
//                    @Override
//                    public void onTagInventory(String uiiStr, final int rssi) {
//                        String tag = uiiStr.replace(" ", "").trim();
//                        //正则判断，是否最低28位的数字
//                        boolean isMatch = Pattern.compile("^\\d{28,}$").matcher(tag).matches();
//                        if (!isMatch) {
//                            //条码不全，或非数字条码
//                            return;
//                        }
//                        String substring1 = uiiStr.substring(4, 26).substring(16);
//                        final String result;
//                        if ("000000".equals(substring1)) {
//                            result = uiiStr.substring(4, 20);
//                        } else {
//                            result = uiiStr.substring(4, 26);
//                        }
//                        new Handler(Looper.getMainLooper()).post(new Runnable() {
//                            @Override
//                            public void run() {
//                                CommonResults commonResults = CommonResultsFactory.successResults();
//                                commonResults.addResult("uiiStr", result);
//                                commonResults.addResult("rssi", rssi);
//                                commonCallback.callback(commonResults);
//                            }
//                        });
//                    }
//
//                    @Override
//                    public void onFinished() {
//
//                    }
//                });
//            }
//        });
//        return commonResults;
//    }
//
//    /**
//     * 停止盘点
//     *
//     * @return
//     */
//    public CommonResults disInventory(CommonParams commonParams, final CommonCallback commonCallback) {
//        CommonResults commonResults = CommonResultsFactory.successResults();
//        try {
//            LableUHFUtil.getInstance().disInventory();
//        } catch (Exception e) {
//            e.printStackTrace();
//            commonResults = CommonResultsFactory.customErrorResults(e.getLocalizedMessage());
//        }
//        return commonResults;
//    }
//
//    /**
//     * 蓝牙是否可用
//     *
//     * @param commonParams
//     * @param commonCallback
//     * @return
//     */
//    public CommonResults isEnable(CommonParams commonParams, final CommonCallback commonCallback) {
//        CommonResults commonResults = CommonResultsFactory.successResults();
//        try {
//            boolean isEnable = LableUHFUtil.getInstance().isEnable();
//            commonResults.addResult("result", isEnable);
//        } catch (Exception e) {
//            e.printStackTrace();
//            commonResults = CommonResultsFactory.customErrorResults(e.getLocalizedMessage());
//        }
//        return commonResults;
//    }
//
//    /**
//     * 打开或 关闭蓝牙
//     */
//    public CommonResults switchBluetoothEnable(CommonParams commonParams, final CommonCallback commonCallback) {
//        boolean isOpen = commonParams.getBoolean("isOpen");
//        CommonResults commonResults = CommonResultsFactory.successResults();
//        try {
//            boolean isOk = LableUHFUtil.getInstance().switchBluetoothEnable(isOpen);
//            commonResults.addResult("result", isOk);
//        } catch (Exception e) {
//            e.printStackTrace();
//            commonResults = CommonResultsFactory.customErrorResults(e.getLocalizedMessage());
//        }
//        return commonResults;
//    }
//
//    /**
//     * 开始扫描
//     *
//     * @param commonParams
//     * @param commonCallback
//     * @return
//     */
//    public CommonResults startDiscovery(CommonParams commonParams, final CommonCallback commonCallback) {
//        CommonResults commonResults = CommonResultsFactory.successResults();
//        try {
//            LableUHFUtil.getInstance().startDiscovery();
//        } catch (Exception e) {
//            e.printStackTrace();
//            commonResults = CommonResultsFactory.customErrorResults(e.getLocalizedMessage());
//        }
//        return commonResults;
//    }
//
//    /**
//     * 取消扫描
//     *
//     * @param commonParams
//     * @param commonCallback
//     * @return
//     */
//    public CommonResults cancelDiscovery(CommonParams commonParams, final CommonCallback commonCallback) {
//        CommonResults commonResults = CommonResultsFactory.successResults();
//        try {
//            LableUHFUtil.getInstance().cancelDiscovery();
//        } catch (Exception e) {
//            e.printStackTrace();
//            commonResults = CommonResultsFactory.customErrorResults(e.getLocalizedMessage());
//        }
//        return commonResults;
//    }

    /**
     * 根据方法名执行对应的方法
     *
     * @param params   参数
     * @param callback 回调
     * @return 结果
     */
    public CommonResults execute(CommonParams params, CommonCallback callback) {
        String methodName = params.getMethodName();
        CommonResults commonResults = CommonResultsFactory.successResults();
        try {
            // 通过反射调用方法，方法参数与返回值必须统一成（CommonParams，CommonCallback，CommonResults）
            Method method = getClass().getMethod(methodName, CommonParams.class, CommonCallback.class);
            commonResults = (CommonResults) method.invoke(this, params, callback);
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.methodInvokeExceptionResults();
            callback.callback(commonResults);
        }
        Log.d(TAG, "发送请求：" + params.toJSON() + "\n"
                + "返回结果：" + (commonResults == null ? "无" : commonResults.toString()));
        return commonResults;
    }
}
